package com.yanqu.road.server.cmd.activity.shuihu;

import com.yanqu.road.entity.activity.shuihu.ShuiHuBox;
import com.yanqu.road.logic.pb.ShuiHuPb;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.activity.ShuiHuActivityProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.cross.battle.ShuiHuActivity;
import com.yanqu.road.server.manager.cross.battle.ShuiHuMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;

@Cmd(code = CrossProtocol.C2_SHUIHU_ACTIVITY_GET_BOX_DATA, desc = "跨服获取奖池数据")
public class CrossGetShuiHuBoxDataCmd extends CrossCommand {

    private int activityId;
    private long userId;
    private long serverId;

    private ShuiHuActivityProto.ShuiHuBoxMsg.Builder shuiHuBoxMsg;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        ShuiHuActivityProto.CrossGetShuiHuBoxDataReqMsg reqMsg = ShuiHuActivityProto.CrossGetShuiHuBoxDataReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        userId = reqMsg.getUserId();
        serverId = reqMsg.getServerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        ShuiHuActivity activity = ShuiHuMgr.getActivity(activityId);
        if (activity == null) {
            ret = GameErrorCode.E_SHUIHU_ACTIVITY_NO_OPEN;
            return;
        }

        ShuiHuBox shuiHuBox = activity.getShuiHuBox();
        if (shuiHuBox == null) {
            ret = GameErrorCode.E_SHUIHU_ACTIVITY_NO_OPEN;
            return;
        }

        shuiHuBoxMsg = ShuiHuPb.parseShuiHuBoxMsg(shuiHuBox, userId, serverId);
    }

    @Override
    public void sendData() {
        ShuiHuActivityProto.GetShuiHuBoxDataRespMsg.Builder builder = ShuiHuActivityProto.GetShuiHuBoxDataRespMsg.newBuilder();
        builder.setRet(ret);
        if (ret == 0) {
            builder.setBoxData(shuiHuBoxMsg);
        }
        MessageHelper.sendPacket(serverId, userId, YanQuMessageUtils.buildMessage(ClientProtocol.U_SHUIHU_ACTIVITY_GET_BOX_DATA, builder));
    }
}
